{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "HyperTS has built-in rich modeling algorithms for different modes, such as:\n",
    "\n",
    "- StatsForecastSearchSpace: Prophet、ARIMA、VAR;\n",
    "- StatsClassificationSearchSpace: TSForest, k-NNs;\n",
    "- DLForecastSearchSpace: DeepAR、RNN、GPU、LSTM、LSTNet;\n",
    "- DLClassificationSearchSpace: RNN、GPU、LSTM、LSTNet.\n",
    "\n",
    "The above algorithms are designed with their own default hyperparameter search spaces. If you want to customize your own search space on this basis, you can specify the customized search space through the parameter ```search_space``` when calling ```make_experiment```."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Suppose now we want to modify the search space of the statistical mode under the forecasting task, namely ```StatsForecastSearchSpace```, you can do the following:\n",
    "\n",
    "- ~Specify the ```task``` in detail, otherwise it is impossible to judge whether it is a univariate or a multivariate forecast task;~\n",
    "- ~Specify the ```timestamp``` column;~\n",
    "- ~If there are covariates, set the parameter ```covariables```;~\n",
    "- ~**Please strictly abide by the above three steps, otherwise the customization will fail!!!**~\n",
    "- If you wish to disable an algorithm and do not search, you can set the parameter to ```False```. For example, ```enable_arima=False```;\n",
    "- If you wish to change the initialization of the search space parameters of an algorithm, you can pass the parameter ```xxx_init_kwargs={xxx:xxx, ...}```;\n",
    "- If you wish your custom parameters to be searchable, you can use ```Choice```, ```Int``` and ```Real``` in ```hypernets.core.search_space```. Where, ```Choice``` supports discrete values, ```Int``` supports integer continuous values, and ```Real``` supports float continuous values. For details, please refer to [Search Space](https://github.com/DataCanvasIO/Hypernets/blob/master/hypernets/core/search_space.py)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Note**\n",
    "\n",
    "- We have updated the custom search space and simplified the parameters.\n",
    "-  See **Simplify**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "02-18 15:58:24 W hyperts.macro_search_space.py 214 - Tip: If other parameters exist, set them directly. For example, covariables=['is_holiday'].\n"
     ]
    }
   ],
   "source": [
    "from hypernets.core.search_space import Choice, Int, Real\n",
    "from hyperts.framework.search_space.macro_search_space import StatsForecastSearchSpace\n",
    "\n",
    "custom_search_space = StatsForecastSearchSpace(task='univariate-forecast', \n",
    "                                               timestamp='TimeStamp',\n",
    "                                               covariables=['HourSin', 'WeekCos', 'CBWD'],\n",
    "                                               enable_arima=False,\n",
    "                                               prophet_init_kwargs={\n",
    "                                                   'seasonality_mode': 'multiplicative',\n",
    "                                                   'daily_seasonality': Choice([True, False]),\n",
    "                                                   'n_changepoints': Int(10, 50, step=10),\n",
    "                                                   'interval_width': Real(0.1, 0.5, step=0.1)}\n",
    "                                               )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from hyperts.datasets import load_network_traffic\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "df = load_network_traffic(univariate=True)\n",
    "train_data, test_data = train_test_split(df, test_size=168, shuffle=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from hyperts import make_experiment\n",
    "\n",
    "experiment = make_experiment(train_data.copy(), \n",
    "                             task='univariate-forecast',\n",
    "                             timestamp='TimeStamp',\n",
    "                             covariables=['HourSin', 'WeekCos', 'CBWD'],\n",
    "                             search_space=custom_search_space) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Simplify "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1. StatsForecastSearchSpace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from hypernets.core.search_space import Choice, Int, Real\n",
    "from hyperts.framework.search_space.macro_search_space import StatsForecastSearchSpace\n",
    "\n",
    "custom_search_space = StatsForecastSearchSpace(enable_arima=False,\n",
    "                                               prophet_init_kwargs={\n",
    "                                                   'seasonality_mode': 'multiplicative',\n",
    "                                                   'daily_seasonality': Choice([True, False]),\n",
    "                                                   'n_changepoints': Int(10, 50, step=10),\n",
    "                                                   'interval_width': Real(0.1, 0.5, step=0.1)}\n",
    "                                               )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from hyperts.datasets import load_network_traffic\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "df = load_network_traffic(univariate=True)\n",
    "train_data, test_data = train_test_split(df, test_size=168, shuffle=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from hyperts import make_experiment\n",
    "\n",
    "experiment = make_experiment(train_data.copy(), \n",
    "                             task='univariate-forecast',\n",
    "                             timestamp='TimeStamp',\n",
    "                             covariables=['HourSin', 'WeekCos', 'CBWD'],\n",
    "                             search_space=custom_search_space) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2. DLForecastSearchSpace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from hypernets.core.search_space import Choice, Int, Real\n",
    "from hyperts.framework.search_space.macro_search_space import DLForecastSearchSpace\n",
    "\n",
    "custom_search_space = DLForecastSearchSpace(enable_deepar=False, \n",
    "                                            enable_hybirdrnn=False, \n",
    "                                            enable_lstnet=False, \n",
    "                                            enable_nbeats=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from hyperts.datasets import load_network_traffic\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "df = load_network_traffic(univariate=True)\n",
    "train_data, test_data = train_test_split(df, test_size=168, shuffle=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from hyperts import make_experiment\n",
    "\n",
    "experiment = make_experiment(train_data.copy(), \n",
    "                             task='univariate-forecast',\n",
    "                             timestamp='TimeStamp',\n",
    "                             covariables=['HourSin', 'WeekCos', 'CBWD'],\n",
    "                             search_space=custom_search_space) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "you alse can..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from hypernets.core.search_space import Choice, Int, Real\n",
    "from hyperts.framework.search_space.macro_search_space import DLForecastSearchSpace\n",
    "\n",
    "custom_search_space = DLForecastSearchSpace(\n",
    "    enable_deepar=False, \n",
    "    enable_hybirdrnn=True, \n",
    "    enable_lstnet=False, \n",
    "    enable_nbeats=True,\n",
    "    hybirdrnn_init_kwargs = {\n",
    "        'rnn_units': Choice([8, 16, 64]),\n",
    "        'rnn_type': Choice(['simple_rnn', 'gru', 'lstm'])\n",
    "    },\n",
    "    nebeats_init_kwargs = {\n",
    "        'nb_blocks_per_stack': Choice([2, 3]),\n",
    "        'hidden_layer_units': Choice([16, 32, 64]),\n",
    "    }\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Note:**\n",
    "For the selection of hyperparameters, refer to:\n",
    "    https://hyperts.readthedocs.io/en/latest/contents/0900_api.html#module-hyperts.framework.estimators"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
